package readcheck.config;

import java.util.List;
import java.util.Map;
import lombok.Data;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import utils.CommonUtils;

/**
 * @Author : orangeCy.奕
 * @Product : 猎豹养成产品源码
 * @Time : 2023/9/2
 * @Desc :
 */
@Data
public class VerifyConfig {

    /**
     * 全量
     */
    private Boolean isFull = Boolean.FALSE;

    /**
     * 白名单
     */
    private String whiteStr;

    /**
     * 黑名单
     */
    private String blackStr;

    /**
     * key:verifyMethod方法全路径
     * value:验证字段
     */
    private Map<String, String> verifyFieldMap;

    /**
     * 是否命中预验证配置
     *
     * @param migrateMethod
     * @return
     */
    public boolean hitPreMigrate(String migrateMethod) {
        try {
            List<String> blackList = CommonUtils.strTransStringList(blackStr, CommonUtils.SEPARATE1);
            if (blackList.contains(migrateMethod)) {
                return false;
            }
            if (Boolean.TRUE.equals(isFull)) {
                return true;
            }
            List<String> whiteList = CommonUtils.strTransStringList(whiteStr, CommonUtils.SEPARATE1);
            if (whiteList.contains(migrateMethod)) {
                return true;
            }
            return false;
        } catch (Exception ignore) {
            return false;
        }
    }

    /**
     * 获取需要验证的字符集
     *
     * @param migrateMethod
     * @return
     */
    public String getVerifyField(String migrateMethod) {
        try {
            if (MapUtils.isEmpty(verifyFieldMap)) {
                return StringUtils.EMPTY;
            }
            return verifyFieldMap.get(migrateMethod) == null ? StringUtils.EMPTY : verifyFieldMap.get(migrateMethod);
        } catch (Exception ignore) {
            return StringUtils.EMPTY;
        }
    }
}
